package com.ruoyi.demo2.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.demo2.domain.Packages;
import com.ruoyi.demo2.mapper.OrdersMapper;
import com.ruoyi.demo2.domain.Orders;
import com.ruoyi.demo2.service.IOrdersService;

/**
 * 订单管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-18
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 查询订单管理
     * 
     * @param ordersId 订单管理主键
     * @return 订单管理
     */
    @Override
    public Orders selectOrdersByOrdersId(Long ordersId)
    {
        return ordersMapper.selectOrdersByOrdersId(ordersId);
    }

    /**
     * 查询订单管理列表
     * 
     * @param orders 订单管理
     * @return 订单管理
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增订单管理
     * 
     * @param orders 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOrders(Orders orders)
    {
        int rows = ordersMapper.insertOrders(orders);
        insertPackages(orders);
        return rows;
    }

    /**
     * 修改订单管理
     * 
     * @param orders 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOrders(Orders orders)
    {
        ordersMapper.deletePackagesByOrdersId(orders.getOrdersId());
        insertPackages(orders);
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单管理
     * 
     * @param ordersIds 需要删除的订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrdersByOrdersIds(Long[] ordersIds)
    {
        ordersMapper.deletePackagesByOrdersIds(ordersIds);
        return ordersMapper.deleteOrdersByOrdersIds(ordersIds);
    }

    /**
     * 删除订单管理信息
     * 
     * @param ordersId 订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrdersByOrdersId(Long ordersId)
    {
        ordersMapper.deletePackagesByOrdersId(ordersId);
        return ordersMapper.deleteOrdersByOrdersId(ordersId);
    }

    /**
     * 新增货物管理信息
     * 
     * @param orders 订单管理对象
     */
    public void insertPackages(Orders orders)
    {
        List<Packages> packagesList = orders.getPackagesList();
        Long ordersId = orders.getOrdersId();
        if (StringUtils.isNotNull(packagesList))
        {
            List<Packages> list = new ArrayList<Packages>();
            for (Packages packages : packagesList)
            {
                packages.setOrdersId(ordersId);
                list.add(packages);
            }
            if (list.size() > 0)
            {
                ordersMapper.batchPackages(list);
            }
        }
    }
}
